// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SERVICE_THREAD_SAFE_MAP_H
#define SERVICE_THREAD_SAFE_MAP_H

#include <map>
#include <list>

#include "AutoRwLock.h"

template<class Key, class Value>
class threadsafe_map {
public:
    threadsafe_map() {
        pthread_rwlock_init(&lock_, NULL);
    }
    virtual ~threadsafe_map() {
        clear();

        pthread_rwlock_destroy(&lock_);
    }

public:
    bool insert(Key k, Value v) {
        AutoWriteLock lock(lock_);

        auto iter = map_.find(k);
        if (iter != map_.end()) {
            return false;
        }
        map_.insert(std::pair<Key, Value>(std::move(k), std::move(v)));
        return true;
    }

    bool insert(std::pair<Key, Value> data) {
        AutoWriteLock lock(lock_);

        auto iter = map_.find(data.first);
        if (iter != map_.end()) {
            return false;
        }
        map_.insert(std::move(data));
        return true;
    }

    bool find(Key k, Value &v) {
        AutoReadLock lock(lock_);

        auto iter = map_.find(k);
        if (iter == map_.end()) {
            return false;
        }
        v = iter->second;
        return true;
    }

    template<typename Obj>
    std::map<Key, Value> get(Obj obj) {
        std::map<Key, Value> mapData;
        {
            AutoReadLock lock(lock_);

            auto iter = map_.begin();
            for (; iter != map_.end(); ++iter) {
                if (!obj(*iter)) {
                    continue;
                }
                mapData.insert(*iter);
            }
        }

        return std::move(mapData);
    }

    template<typename Obj>
    std::map<Key, Value> eraseAll(Obj obj) {
        std::map<Key, Value> mapData;
        {
            AutoWriteLock lock(lock_);

            auto iter = map_.begin();
            while (iter != map_.end()) {
                if (!obj(*iter)) {
                    ++iter;
                } else {
                    mapData.insert(std::move(*iter));
                    iter = map_.erase(iter);
                }
            }
        }

        return std::move(mapData);
    }

    bool existed(Key k) {
        AutoReadLock lock(lock_);

        auto iter = map_.find(k);
        return (iter != map_.end());
    }

    bool erase(Key k) {
        AutoWriteLock lock(lock_);
        return (map_.erase(k) > 0);
    }

    bool erase(Key k, Value &v) {
        AutoWriteLock lock(lock_);

        auto iter = map_.find(k);
        if (iter == map_.end()) {
            return false;
        }
        v = iter->second;
        map_.erase(iter);
        return true;
    }

    void clear() {
        std::map<Key, Value> emptyMap;
        {
            AutoWriteLock lock(lock_);
            std::swap(emptyMap, map_);
        }
    }

private:
    std::map<Key, Value> map_;
    pthread_rwlock_t lock_;
};

#endif //SERVICE_THREAD_SAFE_MAP_H
